package 华为OD机试真题2023.基础题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class HJ46_微服务的集成测试 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/46.txt"));
        int N = in.nextInt();
        int[][] matrix = new int[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                matrix[i][j] = in.nextInt();
            }
        }
        int query = in.nextInt() - 1;
        int res = dfs(matrix, query, N);
        System.out.println(res);
    }

    static int dfs(int[][] matrix, int target, int N) {
        int max = 0;
        int res = matrix[target][target];
        for (int i = 0; i < N; i++) { //注意，i！=target，不能写在这 会短路！
            if (i == target) {
                continue;
            }
            if (matrix[target][i] == 1) {
                max = Math.max(dfs(matrix, i, N), max);
            }
        }
        return max + res;
    }
}
